package com.leetcode.problems031;

/**
 * https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {


    public void nextPermutation(int[] nums) {
        int i = nums.length - 2;
        while (i >= 0 && nums[i] >= nums[i + 1]) {
            i--;
        }
        if (i >= 0) {
            int j = nums.length - 1;
            while (j >= 0 && nums[i] >= nums[j]) {
                j--;
            }
            swap(nums, i, j);
        }
        reverse(nums, i + 1,nums.length - 1);
    }

    public void reverse(int[] nums, int l, int r){
        while (l < r){
            swap(nums,l,r);
            l++;
            r--;
        }
    }

    private void swap(int[] nums, int i, int i2) {
        int temp = nums[i];
        nums[i] = nums[i2];
        nums[i2] = temp;
    }

    public static void main(String[] args) {
        int[] words = {1, 4 ,3,2};
        new Solution().nextPermutation(words);
        for (int word : words) {
            System.out.print(word + ",");
        }
        System.out.println("");
    }
}
